package com.superjson.showpro.controller;

import com.alibaba.fastjson.JSONObject;
import com.beantools.filters.BuilderFilters;
import com.beantools.util.CollectionUtil;
import com.superjson.showpro.entity.Area;
import com.superjson.showpro.service.AreaService;
import com.superjson.showpro.shared.result.Result;
import com.superjson.showpro.shared.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

import static com.superjson.showpro.shared.util.CommonUtil.ifTrueThrow;

/**
 * Created By zby on 23:07 2019/5/21
 */
@RestController("areaController")
@RequestMapping("/area")
public class AreaController {

    @Autowired
    private AreaService areaService;

    /**
     * Created By zby on 13:28 2019/6/5
     * 根据id获取属性集
     */
    @RequestMapping(value = "/childList", method = RequestMethod.GET)
    public Result childList(Long pid) {
        List<Area> childList = areaService.childList(pid).getResultData();
        ifTrueThrow(CollectionUtil.isEmpty(childList), "省市县为空");
        String[] areaJson = {"id", "name"};
        LinkedList<JSONObject> jsons = BuilderFilters.builderPropertyFilters().filerLinkedJsons(areaJson, childList);
        return ResultUtil.buildSuccess(jsons);
    }

}
